<template>
<div class="layerRtb layerRtb-threecolumn">
    <three-title :title="{name:'新增科目'}"></three-title>
    <div class="layerRtb-scroll thinScroll" v-scrollHeight = "137">
        <div class="analyItem">
            <p class="analyItemTit tx-center">类型</p>
            <div class="analyItemCon">
                <el-select v-model="typeValue" placeholder="请选择类型">
                    <el-option
                        size="mini"
                        v-for="item in typeOptions"
                        :key="item.id"
                        :label="item.name"
                        :value="item.id">
                    </el-option>
                </el-select>
                <el-button type="primary" icon="el-icon-plus" circle class="fr" data-title="新增类型" @click="dialogVisible = true"></el-button>
            </div>
        </div>
        <div class="analyItem">
            <p class="analyItemTit tx-center">科目</p>
            <div class="analyItemCon">
                <el-input v-model="projectValue" placeholder="请输入科目" size="mini" style="width:200px"></el-input>
            </div>
        </div>
    </div>
    <div class="layerRtb-footer">
        <div class="tx-center">
            <input type="button" class="uiBtn-normal uiBtn-blue" @click="saveSubjectNew()" value="确定">
        </div>
    </div>
    <el-dialog
        :append-to-body='true'
        title="新增类型"
        :visible.sync="dialogVisible"
        width="300px">
        <div>
            <label class="pr8">类型</label>
            <el-input v-model="addTypeValue" placeholder="请输入类型" size="mini" style="width:200px"></el-input>
        </div>
        <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="addType()">确 定</el-button>
        </span>
    </el-dialog>
</div>
</template>
<script>
import { mapMutations, mapGetters } from 'vuex'
import { getFirstType, saveStandardType } from '../Resources/Api/index'
export default {
    data () {
        return {
            dialogVisible: false, // 弹框显示
            projectName: '', // 科目名称
            projectImgList: '', // 科目图片
            typeOptions: [],
            typeValue: '', // 类型Value
            projectValue: '', // 科目value
            addTypeValue: '' // 新增类型Value
        }
    },
    created () {
        this.getFirstType()
    },
    computed: {
        ...mapGetters(['leftInfo', 'twoToThreeInfo', 'userInfo'])
    },
    methods: {
        ...mapMutations({
            setUpDatePlate: `SET_UPDATE_PLATE`
        }),
        // 图片上传
        successUpload (value) {
            this.projectImgList = value.src
        },
        // 获取类型列表
        getFirstType () {
            getFirstType().then(results => {
                if (results.data.StatusCode === 1) {
                    this.typeOptions = results.data.Body.firstTypeList
                }
            }).catch(error => {
                console.log(error)
            })
        },
        // 新增类型
        addType () {
            let params = {
                name: this.addTypeValue,
                parentId: 0
            }
            saveStandardType(params).then(results => {
                if (results.data.StatusCode === 1) {
                    this.$message('新增类型成功')
                    this.addTypeValue = ''
                    this.dialogVisible = false
                    this.getFirstType()
                }
            }).catch(error => {
                console.log(error)
            })
        },
        // 保存科目
        saveSubjectNew () {
            if (this.projectValue === '' || this.typeValue === '') {
                this.$message('类型和科目不能为空')
                return false
            }
            let params = {
                name: this.projectValue,
                parentId: this.typeValue
            }
            saveStandardType(params).then(results => {
                if (results.data.StatusCode === 1) {
                    this.$message('新增科目成功')
                    this.typeValue = '' // 类型Value
                    this.projectValue = '' // 科目value
                    this.setUpDatePlate()
                }
            }).catch(error => {
                console.log(error)
            })
        }
    }
}
</script>
<style lang="scss">
    .operButton {
        position:absolute;
        top: 50%;
        right: 10px;
        transform:translateY(-50%);
    }
</style>
